<?php
class Default_Model_Users extends Zend_Db_Table{
	protected $_name 	= 'users';
	protected $_primary = 'id';
	
	
	public function itemInSelectbox($arrParam =null, $options = null){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		if($options == null){
			$select = $db->select()
						 ->from('user_group',array('id','group_name'));
			$result = $db->fetchPairs($select);
			$result[0] = ' -- Chọn một nhóm -- ';
			ksort($result);
		}
		return $result;
	}
	public function countItem($arrParam =null, $options = null){
	
		$db = Zend_Registry::get('connectDb');
		$ssFilter = $arrParam['ssFilter'];
		//$db = Zend_Db::factory($adapter,$config);
		$select = $db->select()
					 ->from('users AS u',array('COUNT(u.id) AS totalItem'));
		if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('u.user_name LIKE ?',$keywords,'STRING');
			}
			
		if(!empty($ssFilter['slbgroup'])){
				$select->where('u.group_id =  ?',$ssFilter['slbgroup'],'INTEGER');
			}
		$result = $db->fetchOne($select);
		//echo $result;
		return $result;
	}
	public function listItem($arrParam = null, $options = null){
		$db = Zend_Registry::get('connectDb');
		$paginator = $arrParam['paginator'];
		$ssFilter = $arrParam['ssFilter'];
		
		if($options['task'] == 'admin-list'){
			$select = $db->select()
						 ->from('users AS u',array('id','user_name','first_name','last_name','status','email','register_date'))
						 ->joinLeft('user_group AS g','g.id = u.group_id','group_name');
			
			if(!empty($ssFilter['colum']) && !empty($ssFilter['by'])){
				$select->order($ssFilter['colum'] . ' ' . $ssFilter['by']);
			
			}
									 
			if($paginator['itemCountPerPage']>0){
				$page = $paginator['currentPage'];
				$rowCount = $paginator['itemCountPerPage'];
				$select->limitPage($page,$rowCount);
			}
			
			if(!empty($ssFilter['keywords'])){
				$keywords = '%' . $ssFilter['keywords'] . '%';
				$select->where('u.user_name LIKE ?',$keywords,'STRING');
			}
			if(!empty($ssFilter['slbgroup'])){
				$select->where('u.group_id =  ?',$ssFilter['slbgroup'],'INTEGER');
			}
			//echo $select;
			$result  = $db->fetchAll($select);
		}
		return $result;
	
	}
	public function saveItem($arrParam=null,$options=null){
		if($options['task']== 'admin-add'){
			$encode = new Zendvn_Encode();
			$row = $this->fetchNew();
			$row->user_name 	= $arrParam['user_name'];
			$row->user_avatar 	= $arrParam['user_avatar'];
			$row->email 		= $arrParam['email'];
			$row->password  	= $encode->encode($arrParam['password']);
			$row->first_name 	= $arrParam['first_name'];
			$row->last_name 	= $arrParam['last_name'];
			$row->status 		= $arrParam['status'];
			$row->group_id 		= $arrParam['slbGroup'];
			$row->address 		= $arrParam['address'];
			$row->mobile 		= $arrParam['mobile'];
			$row->birthday 		= date("Y-m-d",strtotime($arrParam['year'] . '-' . $arrParam['month'] . '-' . $arrParam['day']));
			$row->register_date = date("Y-m-d H:i:s");
			$row->register_ip 	= $_SERVER['REMOTE_ADDR'];
			$row->save();
		}
		if($options['task']== 'admin-edit'){
			$where = 'id = ' . $arrParam['id'];
			$row =  $this->fetchRow($where);
			$encode  = new Zendvn_Encode();
			$row->user_name 	= $arrParam['user_name'];
			$row->user_avatar 	= $arrParam['user_avatar'];
			if(!empty($arrParam['password'])){
				$row->password 		= $encode->password($arrParam['password']);
			}
			$row->first_name 	= $arrParam['first_name'];
			$row->last_name 	= $arrParam['last_name'];
			$row->status 		= $arrParam['status'];
			$row->group_id 		= $arrParam['group_id'];
			$row->address 		= $arrParam['address'];
			$row->mobile 		= $arrParam['mobile'];
			$row->birthday 		= date("Y-m-d",strtotime($arrParam['year'] . '-' . $arrParam['month'] . '-' . $arrParam['day']));
			$row->save();
		}
	}
	public function getItem($arrParam=null,$options=null){
		if($options['task']== 'admin-info'||$options['task']== 'admin-edit'){
			$db = Zend_Registry::get('connectDb');
			//$db = Zend_Db::factory($adapter,$config);
			$select = $db->select()
						 ->from('users AS u')
						 ->joinLeft('user_group as g','u.group_id = g.id',array('group_name'))
						 ->where('u.id = ?', $arrParam['id'], 'INTEGER');
			$result = $db->fetchRow($select);
		}
		if($options['task']== 'delete'){
			$db = Zend_Registry::get('connectDb');
			$select = $db->select()
						 ->from('users',array('user_avatar','id'))
						 ->where('id = ?',$arrParam['id']);
			$result = $db->fetchRow($select);
		}
		return $result;
	}
	public function deleteItem($arrParam=null,$options=null){	
			
		if($options['task']== 'admin-delete'){
			$row = $this->getItem($arrParam,array('task'=>'delete'));					
			if(!empty($row['user_avatar'])){
				$upload = new Zendvn_File_Upload();
				$upload->removeFile(FILES_PATH . '/user/orignal/' . $row['user_avatar']);
				$upload->removeFile(FILES_PATH . '/user/img35x35/' . $row['user_avatar']);
				$upload->removeFile(FILES_PATH . '/user/img100x100/' . $row['user_avatar']);
			}
			$where = 'id = '. $arrParam['id'];
			$this->delete($where);
		}
		
		if($options['task']== 'admin-multidelete'){
			$cid = $arrParam['cid'];
			if(count($cid)>0){
			  foreach ($cid as $key=>$value){
			  	$arrParam['id'] = $value;
			  	//LAY TEN HINH ANH CUA AVATAR VA RANKING
				$row = $this->getItem($arrParam,array('task'=>'delete'));
				
				//--------------XOA user_avatar -----------------------
				if(!empty($row['user_avatar'])){
					$upload = new Zendvn_File_Upload();
					$upload->removeFile(FILES_PATH . '/user/orignal/' . $row['user_avatar']);
					$upload->removeFile(FILES_PATH . '/user/img100x100/' . $row['user_avatar']);
					$upload->removeFile(FILES_PATH . '/user/img35x35/' . $row['user_avatar']);
				}
			  }
			  $ids = implode(',',$cid);
			  $where = 'id IN ('.$ids.')';
			 $this->delete($where);	
			}
		}
	}
	public function changeStatus($arrParam=null,$options=nul){
		if($options['task']== 'admin-status'){	
			$cid = $arrParam['cid'];
			if(count($cid)>0){
				if($arrParam['type']==1){
					$status =1 ;
				}else{
					$status =0 ;
				}
				$ids = implode(',',$cid);
				$data = array('status'=>$status);
				$where = 'id IN ('.$ids.')';
				$this->update($data,$where);	
			}
			if($arrParam['id']>0){
				if($arrParam['type'] ==1){
					$status =1 ;
				}else{
					$status =0 ;
				}
				$data = array('status'=>$status);
				$where = 'id ='. $arrParam['id'];
				$this->update($data,$where);
			}
		}
		
	
	}
}